import os
from typing import Generator

import pytest
from fastapi.testclient import TestClient  # 需要requests包
from tortoise.contrib.test import finalizer, initializer  # asynctest

from run import app


@pytest.fixture(scope='module')
def client() -> Generator:
    # setup db for testing
    # db_url = os.environ.get('TEST_DB', 'sqlite://:memory:')
    # initializer(['blog.models'], db_url=db_url)
    initializer(['blog.models'])
    with TestClient(app) as c:
        yield c
    # cleans up the db after testing
    finalizer()


@pytest.fixture(scope='module')
def event_loop(client: TestClient) -> Generator:
    yield client.task.get_loop()
